﻿using System;
using System.Configuration;

namespace Core.Helpers
{
  ///<summary>Configuration settings for this app</summary>
  public static class AppConfig
  {
    ///<summary>Get the value from the app.config file of the host process.</summary>
    ///<param name="key">Key to get. If the key ends with the MachineName of the current computer, that will be retrieved.</param>
    ///<returns>Value from settings. If key not found, returns Empty string.</returns>
    /// <example>
    ///   <appSettings>
    ///     <add key="MySetting" value="Running on some computer."/>
    ///     <add key="MySetting_SunPC111" value="Running on computer 111."/>
    ///     <add key="MySetting_SunPC112" value="Running on computer 112."/>
    ///   </appSettings>
    /// </example>
    public static string GetAppSetting(string key)
    {
      return GetAppSetting(key, string.Empty);
    }

    ///<summary>Get the value from the app.config file of the host process.</summary>
    ///<param name="key">Key to get. If the key ends with the MachineName of the current computer, that will be retrieved.</param>
    ///<param name="defaultValue">Default if key not found</param>
    ///<returns>Value from settings. If key not found, returns defaultValue.</returns>
    /// <example>
    ///   <appSettings>
    ///     <add key="MySetting" value="Running on some computer."/>
    ///     <add key="MySetting_SunPC111" value="Running on computer 111."/>
    ///     <add key="MySetting_SunPC112" value="Running on computer 112."/>
    ///   </appSettings>
    /// </example>
    public static string GetAppSetting(string key, string defaultValue)
    {
      var keyForMachine = string.Format("{0}_{1}", key, Environment.MachineName);

      return ConfigurationManager.AppSettings.Get(keyForMachine).DefaultingTo(
            ConfigurationManager.AppSettings.Get(key).DefaultingTo(
                  defaultValue));
    }
  }
}